package com.dc.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.dc.project.constant.ConstantCommon;
import com.dc.project.entity.bo.TaskDetailBo;
import com.dc.project.entity.map.TaskDetailMapVo;
import com.dc.project.entity.po.Task;
import com.dc.project.entity.po.TaskDetail;
import com.dc.project.entity.query.TaskDetailQuery;
import com.dc.project.entity.vo.TaskDetailVo;
import com.dc.project.mapper.TaskDetailMapper;
import com.dc.project.service.TaskDetailItemService;
import com.dc.project.service.TaskDetailService;
import com.dc.project.service.TemplateService;
import com.dc.project.util.TaskResolveUtil;
import com.own.business.resource.business.service.FileRecordService;
import com.own.component.common.base.service.impl.AbstractBaseService;
import com.own.util.list.ListUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * TaskDetailServiceImpl
 *
 * @author chenxueli
 * @date 2025-03-08 15:11:52
 */
@Slf4j
@Service
public class TaskDetailServiceImpl extends AbstractBaseService<
        TaskDetail,
        TaskDetailBo,
        TaskDetailVo,
        TaskDetailMapVo,
        TaskDetailQuery,
        TaskDetailMapper
        > implements TaskDetailService {

    @Resource
    private TaskDetailMapper taskDetailMapper;

    @Resource
    private FileRecordService fileRecordService;
    @Resource
    private TaskDetailItemService taskDetailItemService;
    @Resource
    private TemplateService templateService;

    @Resource
    private TaskResolveUtil taskResolveUtil;

    /**
     * 根据任务id获取任务详情列表
     *
     * @param taskId 任务id
     * @return 任务详情列表
     */
    @Override
    public List<TaskDetail> listByTaskId(Long taskId) {
        var wrapper = lambdaQueryWrapper()
                .select(
                        TaskDetail::getId,
                        TaskDetail::getFkTaskId,
                        TaskDetail::getTitle,
                        TaskDetail::getType,
                        TaskDetail::getFkFileId,
                        TaskDetail::getFilePath,
                        TaskDetail::getStatus
                )
                .eq(TaskDetail::getFkTaskId, taskId)
                .orderByAsc(TaskDetail::getId);
        return listByWrapper(wrapper);
    }

    /**
     * 根据文件列表创建任务详情信息
     *
     * @param task       任务信息
     * @param fileIdList 文件ID列表
     */
    @Override
    public void createByFileList(Task task, List<Long> fileIdList) {
        // 查询所有的文件信息
        var fileRecordList = fileRecordService.listByIdList(fileIdList);
        // 判断文件是否符合要请求（只允许pdf、word的文件上传）
        var detailList = fileRecordList.stream().map(record -> {
                    var detail = new TaskDetail();
                    detail.setFkTaskId(task.getId());
                    detail.setTitle(record.getOldName());
                    detail.setType(ConstantCommon.Type.CREATE_BY_CUSTOM_FILE);
                    detail.setStatus(TaskDetail.Status.NOT_START);
                    detail.setFkFileId(record.getId());
                    detail.setFilePath(record.getPosition() + record.getPath());
                    return detail;
                })
                .toList();
        // 保存数据
        ListUtil.groupList(detailList, 1000).forEach(taskDetailMapper::insertBatch);
        // 通知处理
        taskResolveUtil.start();
    }

    /**
     * 根据模板key创建任务详情
     *
     * @param task        任务信息
     * @param templateKey 模板key
     */
    @Override
    public void createByTemplateKey(Task task, String templateKey) {
        // 根据模板key获取模板的信息
        var template = templateService.getPoByTemplateKey(templateKey);
        // 只需要创建一份详情信息
        var detail = new TaskDetail();
        detail.setFkTaskId(task.getId());
        detail.setTitle(template.getTitle());
        detail.setType(ConstantCommon.Type.CREATE_BY_TEMPLATE);
        detail.setTemplateKey(templateKey);
        detail.setStatus(TaskDetail.Status.FILE_CONTENT_ANALYSIS_SUCCESS);
        taskDetailMapper.insert(detail);
        // 添加详情项信息
        taskDetailItemService.createByTemplateKey(task.getId(), detail.getId(), templateKey);
    }

    /**
     * 获取第一条未处理的任务详情
     *
     * @return 任务详情
     */
    @Override
    public TaskDetail getFirstNotHandleDetail() {
        var wrapper = lambdaQueryWrapper()
                .eq(TaskDetail::getStatus, TaskDetail.Status.NOT_START)
                .orderByAsc(TaskDetail::getGmtCreate)
                .orderByAsc(TaskDetail::getId);
        return getOneByWrapper(wrapper);
    }

    /**
     * 完成解析
     *
     * @param id     任务详情id
     * @param status 状态
     */
    @Override
    public void complete(Long id, int status) {
        var wrapper = new LambdaUpdateWrapper<TaskDetail>()
                .set(TaskDetail::getStatus, status)
                .eq(TaskDetail::getId, id);
        taskDetailMapper.update(wrapper);
    }

    /**
     * 完成解析
     *
     * @param id      任务详情id
     * @param content 解析内容
     * @param status  解析状态
     */
    @Override
    public void complete(Long id, String content, int status) {
        var wrapper = new LambdaUpdateWrapper<TaskDetail>()
                .set(TaskDetail::getParseMessage, content)
                .set(TaskDetail::getStatus, status)
                .eq(TaskDetail::getId, id);
        taskDetailMapper.update(wrapper);
    }

}
